<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Activity extends CI_Controller {

	public function __construct()
	{
		
		parent::__construct();
		if(!$this->session->userdata('user_id')){
			redirect('/',301);
		}
	}
	
	public function index(){
		redirect('activity/active/');
	}	
	
	public function active($offset=0)
	{
		$data['title']='Activity';
		$paging=5;
		$this->session->set_userdata('activity','activity/active/'.$offset);
		$data['activity']=$this->activities->getAllActivity('',$this->session->userdata('user_id'),'id','DESC',$offset,$paging);
		$num_records = COUNT($this->activities->getAllActivity('',$this->session->userdata('user_id')));  
        if ($num_records > $paging) {
        	    $this->load->library('pagination');
	            $config['base_url']   = site_url('activity/active/');
	            $config['total_rows'] = $num_records;
	            $config['per_page']   = $paging;
	            $config['uri_segment']= 3;
	            $this->pagination->initialize($config);	 
	            $data['pagination']   = $this->pagination->create_links();
        }
		$data['content'] = 'activity-list';
		$this->load->view('index',$data);
	}
	
	public function add(){
		//date_default_timezone_set("Asia/nuku'alofa");
		//echo phpinfo();
		$data['title']='Add Activity';
		$data['customers'] =$this->customers->getAllCustomer('',$this->session->userdata('user_id'),'customer_name','ASC');
		$data['action'] = 'activity/save';
		$data['content'] = 'activity-form';
		$this->load->view('index',$data);
		
	}
	
	function save(){
		if($this->input->post('save')):
              $this->activities->getSave();
              $this->session->set_flashdata('success','success');
              redirect($this->input->server('HTTP_REFERER'));
        else:
              $this->session->set_flashdata('error','error');
              redirect($this->input->server('HTTP_REFERER'));
        endif;	
	}
	
	public function remove($id)
    {
		if($this->activities->getCountActivity($id,$this->session->userdata('user_id'))<1){
			$this->session->set_flashdata('error_message','error');
			if($this->session->userdata('activity'))
				redirect($this->session->userdata('activity'),301);
			else
				redirect('activity/active',301);
		}
		
        $this->activities->remove($id);
		$this->session->set_flashdata('delete_message','success');
		if($this->session->userdata('activity'))
				redirect($this->session->userdata('activity'),301);
		else
				redirect('activity/active',301);
    }
	
	function checkuser(){
		//$Q=$this->db->get('customers');
		//$items=$Q->result_array();
		$items = $data['customers'] =$this->customers->getAllCustomer('',$this->session->userdata('user_id'),'customer_name','ASC');
		foreach ($items as $key => $value) {
				echo "$value[customer_name]|$value[customer_name]\n";
		}
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */